﻿
Public Class [delegate]
    Inherits requestProcessor

    Private Shared startdir As String = Nothing
    Public Const invalidpath As String = "__INVALID_PATH__"

    Public Overrides Sub initial(Optional ByVal config As configParser = Nothing)
        MyBase.initial(config)
        assert(Not config Is Nothing, "delegate should be initialized with a valid configuration.")
        startdir = config.parameter(configNaming.counterDraw.name, configNaming.counterDraw.startdir, _
                                    configNaming.defaultValue.counterDraw.startdir)
        kickRight(startdir, filesystem.directorySeperator)
    End Sub

    Public Overrides Function pathidentity() As String
        Return invalidpath
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        debugassert(False, "should not be able to call delegate.process.")
        Return True
    End Function

    Public Shared Function fullpath(ByVal context As httpContext, _
                                    ByVal pathidlen As Int64) As String
        Dim rtn As String = Nothing
        rtn = relativepath(context, pathidlen)
        kickLeft(rtn, filesystem.directorySeperator)
        rtn = startdir + filesystem.directorySeperator + rtn
        kickRight(rtn, filesystem.directorySeperator)

        Return rtn
    End Function

    Public Shared Function relativepath(ByVal context As httpContext, _
                                        ByVal pathidlen As Int64) As String
        assert(Not context Is Nothing, "context is nothing.")
        Dim rtn As String = Nothing
        rtn = context.url().path
        rtn = strmid(rtn, pathidlen - strlen(network.pathSeperator))
        urlUtils.urlpath2filesystempath(rtn)

        Return rtn
    End Function

    Public Shared Function relativepath(ByVal path As String) As String
        Dim rtn As String = Nothing
        rtn = strmid(path, strlen(startdir))
        kickLeft(rtn, filesystem.directorySeperator)

        Return rtn
    End Function
End Class
